package com.teng.smartdrawer.utils;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * Created by teng on 16/2/19.
 */
public class PreferenceUtils {

    public static final String PREFERENCE_DEFAULT_NAME = "teng";
    private static PreferenceUtils instance;
    SharedPreferences sp;
    SharedPreferences.Editor editor;

    public static final String USER_IS_LOGIN = "user_is_login";
    public static final String USER_NAME = "user_name";
    public static final String USER_HEAD_PIC = "user_head_pic";
    public static final String USER_AUTHORITY = "user_authority";
    public static final String USER_ACCOUNT_NUMBER = "user_account_number";


    private PreferenceUtils(Context _context, String prefName, int mode) {
        sp = _context.getSharedPreferences(prefName, mode);
        editor = sp.edit();
    }

    private PreferenceUtils(Context _context) {
        sp = _context.getSharedPreferences(PREFERENCE_DEFAULT_NAME, Context.MODE_PRIVATE);
        editor = sp.edit();
    }

    public synchronized static PreferenceUtils getInstance(Context _context, String prefName, int mode) {
        if (instance == null) {
            instance = new PreferenceUtils(_context, prefName, mode);
        }
        return instance;
    }

    public static PreferenceUtils getInstance(Context _context, String prefName) {
        return getInstance(_context, prefName, Context.MODE_PRIVATE);
    }

    public static PreferenceUtils getInstance(Context _context) {
        return getInstance(_context, PREFERENCE_DEFAULT_NAME);
    }

    public PreferenceUtils remove(String key) {
        editor = sp.edit();
        editor.remove(key);
        editor.commit();
        return instance;
    }
    //构建器
    /* ----- Boolean ----- */
    public PreferenceUtils addBoolean(String key, Boolean value) {
        editor = sp.edit();
        editor.putBoolean(key, value);
        editor.commit();
        return instance;
    }

    public boolean getBoolean(String key, boolean defValue) {
        return sp.getBoolean(key, defValue);
    }

    /* ----- String ----- */
    public PreferenceUtils addString(String key, String value) {
        editor = sp.edit();
        editor.putString(key, value);
        editor.commit();
        return instance;
    }

    public String getString(String key, String defValue) {
        return sp.getString(key, defValue);
    }

    /* ----- Integer ----- */
    public PreferenceUtils addInt(String key, int value) {
        editor = sp.edit();
        editor.putInt(key, value);
        editor.commit();
        return instance;
    }

    public int getInt(String key, int defValue) {
        return sp.getInt(key, defValue);
    }

}
